<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\frilk;
use App\Http\Requests\frilkPostRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class frilkController extends Controller
{

    /*
        友链的列表
    */

    public function getList(Request $request)
    {
         $data = $request->all();
        //dd($data);
        //$cates = CateController::CateList();
         //调用查询格式的静态
        // 默认显示条数
        $num = $request->input('pnum',2);

        if($request->input('keywords')){
            //有关键字带条件查询
            $frilk = DB::table('frilk')
            ->select('frilk.*')
            ->where('frilk.flkws','like','%'.$request->input('keywords').'%')
            ->paginate($num);

            //dd($frilk);
            
        }else{
                //没有查询全部数据并分页 paginate(显示的条数)
            $frilk = DB::table('frilk')
            ->select('frilk.*')
            ->paginate($num);
            $data['pnum'] = $num; 
            // dd($data);
            //$data['keywords'] = null; 
        }
        // $data['keyword'] = null; 
        // $data['pnum'] = 10; 
        // //提取所有数据
        $data = $request->all();
        //dd($data);
        
        // foreach($frilk as $k => $v){
        //   if($v->pid != 0){
        //         $res = DB::table('cate')->where('id',$v->pid)->first();
        //         // dd($res);
        //         $frilk[$k]->pid = $res->name;
        //     }
        //}
             
       //   dd($frilk);
        // dd($cates);
        //返回视图并,分配变量
        return view('admin.frilk.list',['frilk'=>$frilk,'request'=>$data]);
    }

    /*
        友链的添加
    */
    public function getAdd()
    {
        
        return view('admin.frilk.add');
    }
    

    //执行友链添加
    public function postInsert(Request $request)
    {
        //dd($request->all());
        if($request->hasFile('flpic')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('flpic')->getClientOriginalExtension();
            // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="mao.cn/admin/frilk/list";
                </script>';
            }
            $request->file('flpic')->move('./uploads/',$name.'.'.$ext);
            // echo './uploads/',$name.'.'.$ext;
            // dd(123);
            
           
        }
        //  dd($request->all());
        $frilk = $request->only('flkws','flurl','editorValue');
        $frilk['flpic'] = '/uploads/'.$name.'.'.$ext;
        
        if ($frilk['flpic'] == null) {
            return back()->with('error','请上传图片');
        }
        $result =DB::table('frilk')->insert($frilk);
        //dd($result);
        
            // //执行添加
            if($result){
                //dd($frilk,$request->all());
                
                return redirect('/admin/frilk/list')->with('success','添加友链成功');
            }else{
                return back()->with('failed','友链添加失败');
            }


    }


    /*
        显示友链修改页面
    */
    public function getEdit($id)
    {

        $frilk = DB::table('frilk')
        ->select('frilk.*')
        ->where('frilk.id',$id)->first();
        //dd($cates,$frilk);
        return view('admin.frilk.edit',['frilk'=>$frilk]);
        
    }
    /*
        友链修改
    */
    public function postUpdate(Request $request)
    {
        // dd($request->hasFile('flpic'));
         
        if($request->hasFile('flpic')){
                //随机文件名
                $name = md5(time()+rand(1,9999999));
                //获取后缀名
                $su = $request->file('flpic')->getClientOriginalExtension();
                $arr = array('jpg','png','jpeg','tiff');
                //判断
                if(!in_array($su,$arr)){
                    echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://mao.cn/test/add";
                    </script>';
                }
                //dd($su);
                //将文件移动到指定目录
                $request->file('flpic')->move('./uploads/',$name.'.'.$su);
                //echo '<img src="/upload/'.$name.'.jpg" alt="">';
                $a= '/uploads/'.$name.'.'.$su;
            }
            //dd($request->all());
            //获取id
            $id = $request->only(['id']);

            //详细数据
            $data = $request->only(['flkws','flurl','flpic','editorValue']);
            $data['flpic']= $a;
            //执行修改
            $res = DB::table('frilk')->where('id',$id)->update($data);
            //判断是否添加成功
       if($res){
            return redirect('/admin/frilk/list')->with('success','用户修改成功');

        }else{
             return back()->with('error','用户修改失败');
        }
        
   }

       public function postDelete(Request $request)
    {
     $id = $request->input('id');
     //执行删除
      $res = DB::table('frilk')->where('id',$id)->delete();
      //判断
      if($res){
            echo 1;
       }else{
            echo 0;
       }
    }


}
